const drviceWss = require("./drvice/index");
// const preventErrorToolWss = require("./preventErrorTool/index.cjs");
// const waterjetPropulsionWss = require("./waterjet-propulsion/index.cjs");

// const execRfidTest = require('./rfid-test/index-SerialPort.cjs')
require('./rfid-test/index-tcp-test-2.cjs')
// require('./rfid-test/index-udp.cjs')


module.exports = (server) => {
  server.on("upgrade", function upgrade(request, socket, head) {
    const { pathname } = new URL(request.url, "wss://base.url");

    // 代理ws连接
    const proxyWs = (instance) => {
      instance.handleUpgrade(request, socket, head, (ws) => {
        instance.emit("connection", ws, request);
      });
    };

    // 连接: ws://host:8501/ws/drvice
    switch (pathname) {
      case "/ws/drvice":
        proxyWs(drviceWss);
        break;
      // case "/ws/waterjet-propulsion":
      //   proxyWs(waterjetPropulsionWss);
      //   break;
      // case "/ws/preventErrorTool":
      //   proxyWs(preventErrorToolWss);
      //   break;
      default: {
        socket.destroy();
        // 处理非代理请求
        socket.write("HTTP/1.1 404 Not Found\r\n\r\n");
        socket.end();
      }
    }
  });
};
